package config

import (
	"fmt"
	"os"

	"gopkg.in/yaml.v3"
)

var Config ServerConfig

type ServerConfig struct {
	Port int `yml:"port"`
	Auth struct {
		Token string `yml:"token"`
	} `yml:"auth"`
	Log struct {
		Level string `yml:"level"`
		File  string `yml:"file"`
	} `yml:"log"`
	Service struct {
		Name string `yml:"name"`
	} `yml:"service"`
	File struct {
		Path string `yml:"path"`
	} `yml:"file"`
}

func InitConfig() error {
	// TODO: read config from file
	confiFile, err := os.Open("config.yml")
	if err != nil {
		return fmt.Errorf("failed to open config file: %v", err)
	}
	defer confiFile.Close()
	configDecoder := yaml.NewDecoder(confiFile)
	if err := configDecoder.Decode(&Config); err != nil {
		return fmt.Errorf("failed to decode config file: %v", err)
	}
	return nil
}
